package com.ruoyi.project.api.home;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.banner.domain.Banner;
import com.ruoyi.project.system.banner.service.IBannerService;
import com.ruoyi.project.system.groupimg.domain.GroupImg;
import com.ruoyi.project.system.groupimg.service.IGroupImgService;
import com.ruoyi.project.system.proclamation.domain.Proclamation;
import com.ruoyi.project.system.proclamation.service.IProclamationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * api
 *
 * Created by Administrator.
 *
 * @autor LCL - 2020/8/4 - 16:12.
 */
@Controller
@RequestMapping("/api/home")
@CrossOrigin(origins = "*",maxAge = 3600)
public class ApiHomeController extends BaseController {

    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IProclamationService proclamationService;
    @Autowired
    private IGroupImgService groupImgService;

    /**
     * 查询轮播图
     *
     * @param type
     * @return
     */
    @GetMapping("/banners")
    @ResponseBody
    private R homeBanners(@RequestParam Integer type) {
        Banner queryBan = new Banner();
        queryBan.setType(type);
        return RFactory.generateR(this.bannerService.selectBannerList(queryBan));
    }

    /**
     * 查询预约信息
     *
     * @param proclamation
     * @return
     */
    @PostMapping("/proclamations")
    @ResponseBody
    private R homeProclamations(Proclamation proclamation) {
        Proclamation queryPro = new Proclamation();
        queryPro.setName(proclamation.getName());
        queryPro.setPhone(proclamation.getPhone());
        queryPro.setGrade(proclamation.getGrade());
        queryPro.setSubject(proclamation.getSubject());
        return RFactory.generateR(this.proclamationService.selectProclamationList(queryPro));
    }

    /**
     * 添加预约信息
     *
     * @param proclamation
     * @return
     */
    @PostMapping("/info")
    @ResponseBody
    public R addAppointmentInfo(Proclamation proclamation) {
        Proclamation addInfo = new Proclamation();
        addInfo.setName(proclamation.getName());
        addInfo.setPhone(proclamation.getPhone());
        addInfo.setGrade(proclamation.getGrade());
        addInfo.setSubject(proclamation.getSubject());
        addInfo.setCreated(DateUtils.getNowDate());
        addInfo.setStatus(0);
        this.proclamationService.insertProclamation(addInfo);
        return R.ok();
    }

    /**
     * 查询集团风采图片列表
     *
     * @param type
     * @return
     */
    @GetMapping("/picture")
    @ResponseBody
    public R pictureList(@RequestParam Integer type) {
        startPage();
        GroupImg queryImg = new GroupImg();
        queryImg.setType(type);
        return RFactory.generateR(getDataTable(this.groupImgService.selectGroupImgList(queryImg)));
    }

    /**
     * 分页查询集团风采图片列表
     *
     * @param paramMap
     * @return
     */
    @PostMapping("/groupImg")
    @ResponseBody
    public R queryImg(@RequestBody Map<String, Object> paramMap) {
        if (!paramMap.containsKey("type") || StringUtils.isBlank(paramMap.get("type").toString())) {
            return R.error("缺少参数");
        }
        try {
            PageInfo<GroupImg> info = groupImgService.queryImg(paramMap);
            return R.ok().put("data", info);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("获取失败");
        }
    }

}
